import request from '@/utils/request';

const DISK_API = '/biz/disk';

export interface DiskVO {
  id: string;
  name: string;
  type: string;
  size: number;
  path: string;
  parentId: string;
  sort: number;
  createTime: string;
  updateTime: string;
  createUserId: string;
  updateUserId: string;
}

export interface DiskQuery {
  keywords?: string;
  parentId?: string;
  type?: string;
  cur?: number;
  limit?: number;
}

const DiskAPI = {
	
	getFolderList(queryParams: any) {
	  return request<any, any[]>({
	    url: `${DISK_API}/folder/page`,
	    method: "get",
	    params: queryParams,
	  });
	},
	
	saveFolder(data: any) {
	  return request({
	    url: `${DISK_API}/folder/save`,
	    method: "post",
	    data: data,
	  });
	},
	
	deleteFolderByIds(ids: string) {
	  return request({
	    url: `${DISK_API}/folder/${ids}`,
	    method: "delete",
	  });
	},
	
  /**
   * 获取文件列表
   */
  getPage(params: DiskQuery) {
    return request<any, PageResult<DiskVO>>({
      url: `${DISK_API}/pages`,
      method: 'get',
      params
    });
  },
  
  /**
   * 保存
   *
   * @param data 
   */
  save(data: any) {
    return request({
      url: `${DISK_API}/save`,
      method: "post",
      data: data,
    });
  },

  /**
   * 上传文件
   */
  uploadFile(data: FormData) {
    return request({
      url: `${DISK_API}/upload`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data
    });
  },

  /**
   * 上传文件夹
   */
  uploadFolder(data: FormData) {
    return request({
      url: `${DISK_API}/upload/folder`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data
    });
  },

  /**
   * 删除文件/文件夹
   */
  deleteByIds(ids: string) {
    return request({
      url: `${DISK_API}/${ids}`,
      method: 'delete'
    });
  },

  /**
   * 下载文件
   */
  downloadFile(objectName: string) {
    return request({
      url: `${DISK_API}/download`,
      method: 'get',
      params: { objectName },
      responseType: 'blob'
    });
  },

  /**
   * 获取文件列表
   */
  getList(prefix?: string) {
    return request({
      url: `${DISK_API}/list`,
      method: 'get',
      params: { prefix }
    });
  },

  /**
   * 获取图片预览URL
   */
  getPreviewUrl(objectName: string) {
    return `${import.meta.env.VITE_APP_BASE_API}${DISK_API}/preview/image?objectName=${encodeURIComponent(objectName)}`;
  },

  /**
   * 获取缩略图URL
   */
  getThumbnailUrl(objectName: string, width = 100, height = 100) {
    return `${import.meta.env.VITE_APP_BASE_API}${DISK_API}/preview/thumbnail?objectName=${encodeURIComponent(objectName)}&width=${width}&height=${height}`;
  }
};

export default DiskAPI; 